package org.dicker.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.dicker.biz.BillBiz;
import org.dicker.biz.TypeBiz;
import org.dicker.biz.impl.BillBizImpl;
import org.dicker.biz.impl.TypeBizImpl;
import org.dicker.entity.BillInfo;
import org.dicker.entity.BillSelector;
import org.dicker.entity.TypeInfo;
import org.lanqiao.mvc.entity.ModelAndView;
import org.lanqiao.mvc.entity.YockMvcAnnotation.Controller;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestMapping;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestParam;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseBody;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseDispatch;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

@Controller("/bill")
public class BillController {

	private BillBiz billbiz = new BillBizImpl();
	private TypeBiz typebiz = new TypeBizImpl();
	
	Logger logger = Logger.getLogger(BillController.class);
	
	@RequestMapping(value = "/list.do") 
	@ResponseDispatch(value = "/WEB-INF/console/bill/bill-list.jsp")
	public ModelAndView list(HttpServletRequest req,HttpServletResponse res,
			@RequestParam(name="bill_sel")String bill_sel,
			@RequestParam(name="page",defaultValue = "1")int newpage){
		ModelAndView mv = new ModelAndView();
		try {
			JSONObject jo = JSONUtil.parseObj(bill_sel);
			BillSelector bs = JSONUtil.toBean(jo,BillSelector.class,true);
			//转型,json字符串转为bill选择器
			
			int pagesize = 5;
			int totalcount = billbiz.selectCount(bs);
			int totalpage=totalcount%pagesize==0 ? totalcount/pagesize : totalcount/pagesize+1;
			mv.addObject("totalpage", totalpage);
			mv.addObject("newpage", newpage);
			int groupSize=5;
			//根据当前第几页求出位于第几组
			int currentGroup = (newpage-1)/groupSize + 1;
			int startPage = (currentGroup-1)*groupSize+1;
			int endPage = currentGroup*groupSize;
			mv.addObject("startPage", startPage);
			mv.addObject("endPage", endPage);
			//加入分页数据
			
			bs.setLength(pagesize);
			bs.setStart((newpage-1)*pagesize);
			List<BillInfo> list = billbiz.selectAll(bs);
			//索引数据
			mv.addObject("bill_list", list);
			//并添加数据到页面
			mv.addObject("bill_sel", bs);
			//添加当前查询数据
			
			List<TypeInfo> typelist = typebiz.selectAll();
			mv.addObject("type_list",typelist);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	@RequestMapping(value = "/editpage.do")
	@ResponseDispatch(value = "/WEB-INF/console/bill/bill-edit.jsp")
	public ModelAndView editPage(HttpServletRequest req,HttpServletResponse res,
			@RequestParam(name="bill_id")int bill_id){
		ModelAndView mv = new ModelAndView();
		try {
			BillInfo bi = billbiz.selectOne(bill_id);
			//索引数据
			mv.addObject("bill_info", bi);
			//并添加数据到页面
			
			List<TypeInfo> typelist = typebiz.selectAll();
			mv.addObject("type_list",typelist);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	@RequestMapping(value = "/addpage.do")
	@ResponseDispatch(value = "/WEB-INF/console/bill/bill-edit.jsp")
	public ModelAndView addPage(HttpServletRequest req,HttpServletResponse res){
		ModelAndView mv = new ModelAndView();
		try {
			List<TypeInfo> typelist = typebiz.selectAll();
			mv.addObject("type_list",typelist);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping(value = "/edit.do")
	@ResponseBody
	public String edit(HttpServletRequest req,HttpServletResponse res,
			@RequestParam(name="bill_info")String bill_info){
		String str = "0";
		try {
			JSONObject jo = JSONUtil.parseObj(bill_info);
			BillInfo b = JSONUtil.toBean(jo,BillInfo.class,true);
			//转型,json字符串转为bill选择器
			boolean isok = billbiz.update(b);
			//修改数据
			str=isok?"1":"0";
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	@RequestMapping(value = "/add.do")
	@ResponseBody
	public String add(HttpServletRequest req,HttpServletResponse res,
			@RequestParam(name="bill_info")String bill_info){
		String str = "0";
		try {
			JSONObject jo = JSONUtil.parseObj(bill_info);
			BillInfo b = JSONUtil.toBean(jo,BillInfo.class,false);
			//转型,json字符串转为bill选择器
			boolean isok = billbiz.add(b);
			//修改数据
			str=isok?"1":"0";
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	@RequestMapping(value = "/delete.do")
	@ResponseBody
	public String del(HttpServletRequest req,HttpServletResponse res,
			@RequestParam(name="bill_ids")String bill_ids){
		String str = "0";
		try {
			//转型,json字符串转为bill选择器
			boolean isok = billbiz.delete(bill_ids);
			//修改数据
			str=isok?"1":"0";
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	
}
